<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane"  action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="engineerName" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item" id="area-picker">
                            <div class="layui-form-label">居住地址</div>
                            <div class="layui-input-inline" style="width: 200px;">
                                <select name="engineerProvince"  class="province-selector" data-value="天津市" lay-filter="province-1">
                                    <option value="">请选择省</option>
                                </select>
                            </div>
                            <div class="layui-input-inline" style="width: 200px;">
                                <select name="engineerCity" class="city-selector" data-value="天津市" lay-filter="city-1">
                                    <option value="">请选择市</option>
                                </select>
                            </div>
                            <div class="layui-input-inline" style="width: 200px;">
                                <select name="engineerArea"  class="county-selector" data-value="西青区" lay-filter="county-1">
                                    <option value="">请选择区</option>
                                </select>
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label">手机号</label>
                            <div class="layui-input-inline">
                                <input type="text" lay-verify="phoneOrEmpty" name="engineerPhone" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            {{# if (d.engineerIsEnabled === '1') { }}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-disable" lay-event="delete">禁用</a>
            {{# } else { }}
            <a class="layui-btn layui-btn-xs layui-btn-primary data-count-enable" lay-event="enable">启用</a>
            {{# } }}
        </script>

    </div>
</div>



<script src="https://cdn.staticfile.org/layui/2.6.8/layui.js"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<!--<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>-->


<script>
    layui.use(['form', 'table','layarea'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layarea = layui.layarea;

        layarea.render({
            elem: '#area-picker',

            change: function (res) {
                //选择结果
                console.log(res);
            }
        });


        table.render({
            elem: '#currentTableId',
            url: 'http://localhost:8080/engineer/getAll',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 0},
                {field: 'engineerName', width: 120, title: '姓名'},
                {field: 'engineerPhone', width: 120, title: '手机号'},
                {field: 'engineerLevel', width: 80, title: '等级'},
                {field: 'engineerDescription', title: '个人描述', minWidth: 130},
                {field: 'engineerRegion', width: 200, title: '地区', templet: '#engineerRegionTpl'},
                {title: '操作', minWidth: 30, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            page: true,
            skin: 'line',

        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            data = data.field;

            console.log(data)
            table.reload('currentTableId', {
                url: 'http://localhost:8080/engineer/find', // 重新加载数据的 URL
                method: 'POST',
                where: data,
                page: { // 指定页码和每页显示的数据条数
                    curr: 1,
                    limit: 10
                },
                done: function(res, curr, count) {
                    // 重新加载完成后的回调函数
                    console.log('重新加载完成');
                }
            });
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加工程师',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['60%', '50%'],
                    content: '../page/table/engineerAdd.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });


            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                // layer.alert(JSON.stringify(data));
                layer.confirm('真的禁用选中的人嘛？', function (index) {
                    for (var i = 0; i < data.length; i++) {
                        var obj = data[i];

                        axios.put('http://localhost:8080/engineer/' + obj.engineerId + '/setEnabled/0', {
                            withCredentials: true

                        }).then(function(res) {
                            layer.msg("禁用成功");
                            // 刷新表格数据
                            table.reload('currentTableId');

                        }).catch(function (e){
                            layer.msg("禁用失败")
                        })
                    }
                    layer.close(index);
                });

            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var engineerId = data.engineerId;
            console.log(engineerId)

            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['80%', '75%'],
                    content: '../page/table/engineerEdit.html?engineerId='+data.engineerId,

                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                layer.confirm('确定要禁用这个工程师吗？', function (index) {
                    $.ajax({
                        url: 'http://localhost:8080/engineer/' + engineerId + '/setEnabled/0',
                        type: 'PUT',
                        success: function (response) {
                            // 设置成功的回调处理
                            layer.msg("禁用成功");
                            // 刷新表格数据
                            table.reload('currentTableId');
                        },
                    /*
                    $.ajax({
                        url: 'http://localhost:8080/engineer/'+engineerId,
                        type: 'DELETE',

                        success: function(response) {
                            // 处理成功响应
                            layer.msg("禁用成功")
                        },*/
                        error: function(xhr, status, error) {
                            layer.msg("操作失败")
                        }
                    });
               //     obj.del();
                    layer.close(index);
                });
            }
            else if (obj.event === 'enable') {
                layer.confirm('确定要启用这个工程师吗？', function (index) {
                    $.ajax({
                        url: 'http://localhost:8080/engineer/' + engineerId + '/setEnabled/1',
                        type: 'PUT',
                        success: function (response) {
                            // 设置成功的回调处理
                            layer.msg("启用成功");
                            // 刷新表格数据
                            table.reload('currentTableId');
                        },
                        /*
                        $.ajax({
                            url: 'http://localhost:8080/engineer/'+engineerId,
                            type: 'DELETE',

                            success: function(response) {
                                // 处理成功响应
                                layer.msg("禁用成功")
                            },*/
                        error: function(xhr, status, error) {
                            layer.msg("操作失败")
                        }
                    });
                    //     obj.del();
                    layer.close(index);
                });
            }
        });

    });
</script>
<script type="text/html" id="engineerRegionTpl">
    {{ d.engineerProvince }} {{ d.engineerCity }} {{ d.engineerArea }}
</script>

</body>
</html>